﻿namespace VaccineHost.Extend
{
    /// <summary>
    /// 处理Option预请求的中间件
    /// </summary>
    public class PreOptionRequestMiddleware
    {
        private readonly RequestDelegate _next;

        public PreOptionRequestMiddleware(RequestDelegate next)
        {
            _next = next;
        }
        /// <summary>
        /// 中间件处理
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public async Task Invoke(HttpContext context)
        {
            // 当请求类型为OPTIONS直接返回200：跨越预请求处理
            if (context.Request.Method.ToUpper() == "OPTIONS")
            {
                context.Response.StatusCode = 200;
                return;
            }
            await _next.Invoke(context);
        }
    }
}
